<template>
  <div id="QuestionListView">
    <a-form :model="searchParams" layout="inline">
      <a-form-item field="title" label="title">
        <a-input
          v-model="searchParams.title"
          placeholder="please enter title..."
          style="min-width: 300px"
        />
      </a-form-item>
      <a-form-item field="tags" label="tags"
        ><a-input-tag
          v-model="searchParams.tags"
          style="min-width: 300px"
          placeholder="Please enter tags..."
          allow-clear
        />
      </a-form-item>
      <a-form-item>
        <a-button type="second" @click="doSearch">Search</a-button>
      </a-form-item>
    </a-form>

    <a-table
      :columns="columns"
      :data="dataList"
      :pagination="{
        pageSize: searchParams.pageSize,
        current: searchParams.current,
        total: total,
        showTotal: true,
      }"
      @page-change="onPageChange"
    >
      <template #tags="{ record }">
        <a-space wrap>
          <a-tag
            v-for="(tag, index) of record.tags"
            :key="index"
            color="grey"
            >{{ tag }}</a-tag
          >
        </a-space>
      </template>

      <template #acceptedRate="{ record }"
        >{{
          `${
            record.submitNum ? record.submitNum / record.acceptedNum : "0"
          }    (${record.acceptedNum} / ${record.submitNum})`
        }}
      </template>

      <template #createTime="{ record }">
        {{ moment(record.createTime).format("YYYY-MM-DD") }}</template
      >

      <template #optional="{ record }">
        <a-space>
          <a-button @click="toQuestionPage(record)" status="success"
            >GOTO</a-button
          >
        </a-space>
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref, watch, watchEffect } from "vue";
import {
  Page_Question_,
  Question,
  QuestionControllerService,
  QuestionQueryRequest,
} from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import moment from "moment";

const layout = ref("horizontal");
const form = reactive({
  name: "",
  post: "",
  isRead: false,
});

const router = useRouter();

const dataList = ref([]);

const total = ref();

const columns = [
  {
    title: "id",
    dataIndex: "id",
  },
  {
    title: "title",
    dataIndex: "title",
  },
  {
    title: "tags",
    slotName: "tags",
  },
  {
    title: "acceptedRate",
    slotName: "acceptedRate",
  },
  {
    title: "createTime",
    slotName: "createTime",
  },
  {
    title: "Optional",
    slotName: "optional",
  },
];

const searchParams = ref<QuestionQueryRequest>({
  title: "",
  tags: [],
  pageSize: 10,
  current: 1,
});

const onPageChange = (page: number) => {
  searchParams.value = { ...searchParams.value, current: page };
};

const toQuestionPage = async (question: Question) => {
  router.push({
    path: `/view/question/${question.id}`,
    query: {
      id: question.id,
    },
  });
  loadData();
};

const loadData = async () => {
  const res = await QuestionControllerService.listQuestionVoByPageUsingPost(
    searchParams.value
  );
  if (res.code === 0) {
    dataList.value = res.data.records;
    total.value = res.data.total;
  } else {
    message.error("require dataList failed!", res.message);
  }
};

const doSearch = () => {
  searchParams.value = { ...searchParams.value, current: 1 };
};

//当loadData函数中的变量发送变化的时候， 就会自动执行loadData
watchEffect(() => {
  loadData();
});

onMounted(() => {
  loadData();
});
</script>
